package com.renrenyou.duanshipin.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.renrenyou.duanshipin.R;


//固件更新进度
public class UpgradProgressView extends View {
    private final Paint mPaint;
    private int mProgress;//取值范围[0-100]
    private final int mTotalColor, mProgressColor;

    public UpgradProgressView(Context context) {
        this(context, null);
    }

    public UpgradProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UpgradProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UpgradProgressView);
        mTotalColor = a.getColor(R.styleable.UpgradProgressView_upv_color_total, Color.WHITE);
        mProgressColor = a.getColor(R.styleable.UpgradProgressView_upv_color_progress, Color.BLUE);
        a.recycle();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    public void setProgress(int progress) {
        this.mProgress = Math.min(progress, 100);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getMeasuredWidth();
        float y = getMeasuredHeight() / 2f;

        mPaint.setStrokeWidth(y * 2);
        mPaint.setColor(mTotalColor);
        canvas.drawLine(y, y, width - y, y, mPaint);

        if (mProgress > 0L) {
            mPaint.setColor(mProgressColor);
            canvas.drawLine(y, y, mProgress * (width - y) / 100f, y, mPaint);
        }
    }
}
